import { Pipe, PipeTransform } from '@angular/core';

/**
 * 将时间戳(秒)转为 正常日期时间格式
 */
@Pipe({
    name: 'textEllipsis'
})
export class TextEllipsisPipe implements PipeTransform {
    transform(value: string): string {
        let result = '';
        let newLength = 0;
        let charCode = 0;
        for (let i = 0; i < value.length; i++) {
            if (newLength >= 180) {
                result = result + '...';
                break;
            } else {
                charCode = value.charCodeAt(i);
                if (charCode === 94 || charCode > 127) {
                    newLength += 2;
                } else {
                    newLength += 1;
                }
                result += value.substring(i, i + 1);
            }
        }
        return result;
    }
}
